import { reactive, onMounted } from 'vue'
import { format } from 'date-fns'
import { parseTime } from '@/common/common'
import { Project } from '@/service/modules/project/types'
import { createProDbReq } from '@/service/modules/data-manage'
import {
  getProjectListReq,
  projectArchivedReq
} from '@/service/modules/project'
import { useRouter } from 'vue-router'

export function useTable() {
  const router = useRouter()

  const state = reactive({
    page: 1,
    limit: 10,
    count: 0,
    searchParam: {
      name: '',
      departmentId: null
    },
    list: [],
    loading: false,
    currentRecord: {} as Project,
    modAliasModalShow: false,
    modDescModalShow: false,
    modDepartmemtModalShow: false,
    createUserModalShow: false,
    setHiveDbSizeModalShow: false,
    setProjectManagerModalShow: false
  })

  const getList = async () => {
    if (state.loading) return
    state.loading = true

    const { data, count } = await getProjectListReq({
      page: state.page,
      limit: state.limit,
      ...state.searchParam
    })
    state.loading = false
    if (!data) throw Error()
    state.list = data.map((record: Project) => {
      record.createTime = record.createTime
        ? format(parseTime(record.createTime), 'yyyy-MM-dd HH:mm:ss')
        : ''
      record.updateTime = record.updateTime
        ? format(parseTime(record.updateTime), 'yyyy-MM-dd HH:mm:ss')
        : ''
      return record
    })
    state.count = count
  }

  const updateList = () => {
    if (state.list.length === 1 && state.page > 1) {
      --state.page
    }
    getList()
  }

  const createProDb = async (proName: string) => {
    await createProDbReq({ proName: proName })
  }
  const openProMenberTab = (proId: number, proName: string) => {
    router.push({
      path: '/project/proMenber',
      query: {
        tab: `${proName} ` + window.$t('project.proMenber'),
        tabName: 'ProUserManageTab',
        proId: proId
      }
    })
  }
  const projectArchived = async (proId: number, proName: string) => {
    window.$dialog.warning({
      title: window.$t('dialog.warningTitle'),
      content: window.$t('project.projectArchivedTips') + proName,
      positiveText: window.$t('dialog.positiveText'),
      negativeText: window.$t('dialog.negativeText'),
      onPositiveClick: async () => {
        const res = await projectArchivedReq({ proId: proId })
        if (res.resp_code === 0) {
          getList()
        }
      }
    })
  }

  const onOperationClick = (
    data: { rowData: Project },
    type:
      | 'modAlias'
      | 'modDesc'
      | 'modDepartment'
      | 'openProMenberTab'
      | 'setProjectManager'
      | 'createProDb'
      | 'setProDbSize'
      | 'projectArchived'
  ) => {
    state.currentRecord = data.rowData
    switch (type) {
      case 'modAlias':
        state.modAliasModalShow = true
        break
      case 'modDesc':
        state.modDescModalShow = true
        break
      case 'modDepartment':
        state.modDepartmemtModalShow = true
        break
      case 'openProMenberTab':
        openProMenberTab(data.rowData.id, data.rowData.name)
        break
      case 'setProjectManager':
        state.setProjectManagerModalShow = true
        break
      case 'createProDb':
        createProDb(data.rowData.name)
        break
      case 'setProDbSize':
        state.setHiveDbSizeModalShow = true
        break
      case 'projectArchived':
        projectArchived(data.rowData.id, data.rowData.name)
        break
    }
  }

  const changePage = (page: number) => {
    state.page = page
    getList()
  }

  const changePageSize = (limit: number) => {
    state.page = 1
    state.limit = limit
    getList()
  }

  onMounted(() => {
    getList()
  })

  return {
    state,
    changePage,
    changePageSize,
    updateList,
    onOperationClick
  }
}
